import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let store = new Vuex.Store({
  state: {
    carPanelData: []
  },
  getters:  {
    totalCount (state) {
      let count = 0;
      state.carPanelData.forEach((goods) => {
        count += goods.count
      });
      return count;
    },
    totalPrice (state){
      let price = 0;
      state.carPanelData.forEach((goods) =>{
        price += goods.price * goods.count
      })
      return price
    }

  },
  mutations: {
    addCarPanelData (state,data) {
      let bOff = true
      state.carPanelData.forEach((goods) => {
        if (goods.sku_id === data.sku_id) {
          goods.count++
          bOff = false
        }
      })
      if (bOff) {
        let goodsData = data
        Vue.set(goodsData, 'count', 1)
        state.carPanelData.push(goodsData)
      }
      console.log(state.carPanelData);
      /* 
      Vue.set(target,key,value)
      向*响应式*对象中添加一个*响应式*的属性,触发视图更新
      对象不能是Vue实例,或者vue实例的根数据对象
      */
    }
  }
})

export default store